package com.mec.web.y2012.coreservlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

@WebServlet("/DatabaseInfoServlet")
public class DatabaseInfoServlet extends HttpServlet {
	private static final long serialVersionUID = -3733938931837297402L;
	
	final String errorAttr = "error";
	final String connAttr = "connection";
	final String jdbcDriverParam = "jdbcDriver";
	final String dbUrlParam = "dbUrl";
	final String userNameParam = "userName";
	final String passwordParam = "password";
	Connection conn = null;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String forwardPath = "el/dataBaseInfo.jsp";
		
		//
		String jdbcDriver = req.getParameter(jdbcDriverParam);
		String dbUrl = req.getParameter(dbUrlParam);
		String userName = req.getParameter(userNameParam);
		String password = req.getParameter(passwordParam);
		//
		
		
		
		
		try {
			DBUtilities dbUtil = DBUtilities.getInstance();
			if(
					!(
							null == jdbcDriver
							|| null == dbUrl
							|| null == userName
							|| null == password
							)
					){
				dbUtil.setConnectionInfo(jdbcDriver, dbUrl, userName, password);
			}else{
				dbUtil.setConnectionInfo("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/shop", "scott", "tiger");
				
			}
			conn = dbUtil.getConnection();
		} catch (SQLException se) {
			logger.error(se);
			setReqException(req, se);
		}
		
		//
		if(null != conn){
			try {
				DatabaseMetaData dbMetaData = conn.getMetaData();
				String productName = dbMetaData.getDatabaseProductName();
				String productVersion = dbMetaData.getDatabaseProductName();
				int majorVersion = dbMetaData.getDatabaseMajorVersion();
				int minorVersion = dbMetaData.getDatabaseMinorVersion();
				
				req.setAttribute("productName", productName);
				req.setAttribute("productVersion", productVersion);
				req.setAttribute("majorVersin", majorVersion);
				req.setAttribute("minorVersion", minorVersion);
			} catch (SQLException se) {
				setReqException(req, se);
			}
		}else{
			setReqString(req, "Null Database Connection.");
		}
		
		//
		req.setAttribute(connAttr, conn);
		RequestDispatcher dispatcher = req.getRequestDispatcher(forwardPath);
		dispatcher.forward(req, resp);
		
		//
	}
	
	
	
	@Override
	public void destroy() {
		if(null != conn){
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("Close database connection failed.", e);
			}
		}
		super.destroy();
	}



	private void setReqException(HttpServletRequest req, Exception e){
		req.setAttribute(errorAttr, e.getStackTrace());
	}
	private void setReqString(HttpServletRequest req, String s){
		req.setAttribute(errorAttr, s);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}


	private static final Logger logger = 
			Logger.getLogger(DatabaseInfoServlet.class);

}
